package cn.liziguo.tank.collision;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.BulletType;
import cn.liziguo.tank.constants.CampEnum;
import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.constants.PropertyConst;
import cn.liziguo.tank.factory.EffectsFactory;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import javafx.util.Duration;

/**
 * @author Liziguo
 * @date 2022-07-09 21:26:11
 */
public class BulletBorderCollisionHandler extends CollisionHandler {

    /**
     * 延迟时间1秒
     */
    public static final Duration DELAY = Duration.seconds(1);

    public BulletBorderCollisionHandler() {
        super(GameEnum.BULLET, GameEnum.BORDER);
    }

    @Override
    protected void onCollisionBegin(Entity bullet, Entity border) {
        Object type = bullet.getPropertyOptional(PropertyConst.BULLET_TYPE).orElseThrow();
        if (type == BulletType.NORMAL) {
            // 普通子弹碰撞游戏边界1秒钟后将其移除
            FXGL.getGameTimer().runOnceAfter(bullet::removeFromWorld, DELAY);
        } else {
            // 超级子弹碰撞游戏边界立即移除
            bullet.removeFromWorld();
            boolean camp = bullet.getObject(PropertyConst.CAMP) == CampEnum.PLAYER;
            EffectsFactory.createBigBoom(bullet.getCenter(), GameConfig.BULLET_PLUS_BOOM_SIZE, camp, GameConfig.BULLET_PLUS_BOOM_ATTACK);
        }

    }
}
